package com.nkym.wypan.controller;

import cn.hutool.core.text.StrSplitter;
import cn.hutool.core.util.StrUtil;
import com.nkym.wypan.annotation.CheckShareCode;
import com.nkym.wypan.annotation.LoginIgnore;
import com.nkym.wypan.constants.FileConstants;
import com.nkym.wypan.enums.ResponseStatus;
import com.nkym.wypan.exception.BusinessException;
import com.nkym.wypan.model.context.share.*;

import com.nkym.wypan.model.req.share.*;
import com.nkym.wypan.model.resp.share.*;

import com.nkym.wypan.model.resp.userfile.BreadcrumbVo;
import com.nkym.wypan.model.resp.userfile.QueryUserFileVo;
import com.nkym.wypan.response.BaseResponse;
import com.nkym.wypan.service.ShareService;
import com.nkym.wypan.utils.ShareContextHolder;
import com.nkym.wypan.utils.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @ClassName ShareController
 * @Author nkym
 * @Date 2023/8/16 17:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/share")
@Api(tags = "share module")
public class ShareController {


    @Resource
    ShareService shareService;

    @PostMapping("/create-share")
    @ApiOperation(
            value = "创建分享链接",
            notes = "创建分享链接操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public BaseResponse<CreateShareVO> createShare(@Valid @RequestBody CreateShareReq req) {
        String shareName = req.getShareName();
        Integer shareType = req.getShareType();
        Integer shareDayType = req.getShareDayType();
        String fileList = req.getFileList();
        String shareCode = req.getShareCode();
        List<Long> list = StrSplitter.split(fileList, FileConstants.SEPARATOR, 0, true, true).stream()
                .map(Long::parseLong).collect(Collectors.toList());

        CreateShareContext context = new CreateShareContext();
        context.setShareName(shareName);
        context.setShareType(shareType);
        context.setShareDayType(shareDayType);
        context.setShareCode(shareCode);
        context.setFileIdList(list);
        context.setUserId(UserContextHolder.getId());

        CreateShareVO res = shareService.createShare(context);
        return BaseResponse.ok(res);
    }

    @GetMapping("/shares")
    @ApiOperation(
            value = "获取分享列表",
            notes = "获取分享列表操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public BaseResponse<List<ShareVO>> shares() {
        List<ShareVO> res = shareService.shares();
        return BaseResponse.ok(res);
    }

    @PostMapping("/cancel")
    @ApiOperation(
            value = "取消分享",
            notes = "取消分享操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public BaseResponse cancel(@Valid @RequestBody CancelShareReq req) {
        String shareIds = req.getShareIds();
        List<Long> shareIdList = StrUtil.split(shareIds, FileConstants.SEPARATOR, true, true).stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());


        CancelShareContext context = new CancelShareContext();
        context.setShareIdList(shareIdList);
        context.setUserId(UserContextHolder.getId());
        shareService.cancel(context);
        return BaseResponse.ok();
    }

    @PostMapping("/check-code")
    @ApiOperation(
            value = "分享码校验",
            notes = "分享码校验操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @LoginIgnore
    public BaseResponse<CheckCodeVO> checkCode(@RequestBody @Valid CheckCodeReq req) {
        CheckCodeVO res = shareService.checkCode(req);
        return BaseResponse.ok(res);
    }

    @PostMapping("/detail-share")
    @ApiOperation(
            value = "通过分享链接获取具体分享列表",
            notes = "通过分享链接获取具体分享列表操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @CheckShareCode
    @LoginIgnore
    public BaseResponse<DetailShareVO> detailShare() {
        DetailShareContext context = new DetailShareContext();
        context.setShareId(ShareContextHolder.getId());
        DetailShareVO vo = shareService.shareDetail(context);
        return BaseResponse.ok(vo);
    }

    @GetMapping("/simple-share")
    @ApiOperation(
            value = "通过分享链接获取简单信息",
            notes = "通过分享链接获取简单信息操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @LoginIgnore
    public BaseResponse<SimpleShareVO> simpleShare(Long shareId) {
        if (shareId == null) {
            throw new BusinessException(ResponseStatus.VERIFY_CODE_INVALID);
        }
        DetailShareContext context = new DetailShareContext();
        context.setShareId(shareId);
        SimpleShareVO vo = shareService.simpleShare(context);
        return BaseResponse.ok(vo);
    }

    @PostMapping("/next-folder")
    @ApiOperation(
            value = "获取下一级目录",
            notes = "获取下一级目录操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @CheckShareCode
    @LoginIgnore
    public BaseResponse<List<QueryUserFileVo>> nextFolder(@RequestBody @Valid NextFolderReq req) {
        Long parentId = req.getParentId();
        Long shareId = ShareContextHolder.getId();
        NextFolderContext context = new NextFolderContext();
        context.setParentId(parentId);
        context.setShareId(shareId);
        List<QueryUserFileVo> res = shareService.nextFolder(context);
        return BaseResponse.ok(res);
    }

    @PostMapping("/save-share")
    @ApiOperation(
            value = "保存分享",
            notes = "保存分享操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @CheckShareCode
    public BaseResponse saveShare(@RequestBody @Valid SaveShareReq req) {
        String fileIds = req.getFileIds();
        List<Long> fileIdList = StrUtil.split(fileIds, FileConstants.SEPARATOR, true, true).stream()
                .map(Long::parseLong).collect(Collectors.toList());
        Long shareId = ShareContextHolder.getId();
        Long userId = UserContextHolder.getId();
        SaveShareContext context = new SaveShareContext();
        context.setFileIdList(fileIdList);
        context.setShareId(shareId);
        context.setUserId(userId);
        context.setFolderId(req.getFolderId());
        shareService.saveShare(context);
        return BaseResponse.ok(ResponseStatus.SUCCESS);
    }

    @GetMapping("/download/{code}")
    @ApiOperation(
            value = "下载",
            notes = "下载操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @LoginIgnore
    public void download(@PathVariable("code") String code, HttpServletResponse response) {
        ShareDownloadContext shareDownloadContext = new ShareDownloadContext();
        shareDownloadContext.setResponse(response);
        shareDownloadContext.setCode(code);
        shareService.download(shareDownloadContext);
    }

    @PostMapping("/download/get-code")
    @ApiOperation(
            value = "获取下载码",
            notes = "获取下载码操作",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @CheckShareCode
    @LoginIgnore
    public BaseResponse<GetCodeVO> getDownloadCode(@RequestBody @Valid GetCodeReq req) {
        GetCodeContext codeContext = new GetCodeContext();
        codeContext.setFileId(req.getFileId());
        Long shareId = ShareContextHolder.getId();
        codeContext.setShareId(shareId);
        String code = shareService.getDownloadCode(codeContext);
        GetCodeVO getCodeVO = new GetCodeVO();
        getCodeVO.setCode(code);
        return BaseResponse.ok(getCodeVO);
    }

    @PostMapping("/list-folder")
    @ApiOperation(
            value = "获取当前用户所有目录文件",
            notes = "获取所有目录文件",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public BaseResponse<List<FolderVO>> listFolder(@RequestBody @Valid ListFolderReq req) {
        ListFolderContext context = new ListFolderContext();
        context.setParentId(req.getParentId());
        context.setUserId(UserContextHolder.getId());
        List<FolderVO> res = shareService.listFolder(context);
        return BaseResponse.ok(res);
    }

    @PostMapping("/breadcrumb")
    @ApiOperation(
            value = "获取面包屑",
            notes = "提供文获取面包屑服务"
    )
    @LoginIgnore
    @CheckShareCode
    public BaseResponse<List<BreadcrumbVo>> breadcrumb(@RequestBody @Valid
                                                       BreadcrumbReq req) {
        Long fileId = req.getFileId();
        BreadcrumbContext breadcrumbContext = new BreadcrumbContext();
        breadcrumbContext.setFileId(fileId);
        breadcrumbContext.setShareId(ShareContextHolder.getId());
        List<BreadcrumbVo> res = shareService.breadcrumb(breadcrumbContext);
        return BaseResponse.ok(res);
    }


}
